package cc.booku.owlbox.ThanksDonating;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.List;

import cc.booku.owlbox.ArtDesign.SystemUtil;
import cc.booku.owlbox.FileOperations.FileoOperations;
import cc.booku.owlbox.R;

public class ThanksDonatingActivity extends AppCompatActivity {


    private Toolbar Toolbar;

    private thanksD_C thanksD_c;
    private thanksD_A vodClassA;
    private ListView thanksD;
    private List<thanksD_C> thanksD_cArrayList = new ArrayList<thanksD_C>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_thanks_donating);
        SystemUtil.setStatusBarColor(this, Color.parseColor("#448aca"));
        thanksD = (ListView) findViewById(R.id.thanks_t);

        LoadToolbar();

        //读取txt文件
        init_thanksdonating();

    }

    private void LoadToolbar() {
        //新页面接收数据


        Toolbar = (Toolbar) findViewById(R.id.toolbar);

        Toolbar.setTitle("所有捐赠");

        setSupportActionBar(Toolbar);//利用Toolbar代替ActionBar
        Toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转到用户设置页面
                finish();
            }
        });

    }


    private void init_thanksdonating() {

        String ThanksDonating = FileoOperations.readAssetsTxt(this, "TxtData/BillBoard");
        JSONObject obja = JSONObject.parseObject(ThanksDonating);
        String ThanksDonating_res = obja.getString("捐赠名单");

        // String result =“内地综艺,剧情,动作,动漫电影,台湾,喜剧,国产,国产动漫,微电影,恐怖,战争,日本,日韩动漫,日韩综艺,欧美,欧美动漫,欧美综艺,泰国,海外,海外动漫,港台动漫,港台综艺,爱情,电影解说,科幻,纪录,综艺,解说,韩国,香港”;
        String[] str = ThanksDonating_res.split(",");
        for (String string : str) {
            thanksD_c = new thanksD_C();
            thanksD_c.setClass_txt(string);
            thanksD_cArrayList.add(thanksD_c);
        }
        Message msg = new Message();
        msg.what = 2;
        handler.sendMessage(msg);//用activity中的handler发送消息


    }


    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 2:
                    thanksDClickListener((ArrayList<thanksD_C>) thanksD_cArrayList);
                    break;
            }
        }
    };

    public void thanksDClickListener(ArrayList<thanksD_C> thanksD_cs) {


        vodClassA = new thanksD_A(R.layout.thanks_item, this, thanksD_cs);

        thanksD.setAdapter(vodClassA);
        thanksD.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                //Toast.makeText(getContext(), "Click ++ item" + i, Toast.LENGTH_SHORT).show();
                final TextView tv_str = (TextView) view.findViewById(R.id.tv_str);
                String tv_str1 = tv_str.getText().toString();
                //拿到字，你要做的事，在这里写，activity跳转，settext之类的
            }
        });

    }


    class thanksD_C {

        private String class_txt;

        public String getClass_txt() {
            return class_txt;
        }

        public void setClass_txt(String class_txt) {
            this.class_txt = class_txt;
        }
    }

    class thanksD_A extends BaseAdapter implements ListAdapter {
        private ArrayList<thanksD_C> thanksD_cs;
        private int id;
        private Context context;
        private LayoutInflater inflater;

        public thanksD_A(int subpage_item, ThanksDonatingActivity context, ArrayList<thanksD_C> thanksD_cs) {
            this.thanksD_cs = thanksD_cs;
            this.context = context;
            this.id = subpage_item;
            inflater = LayoutInflater.from(context);

        }

        @Override
        public int getCount() {
            return thanksD_cs.size();
        }

        @Override
        public Object getItem(int i) {
            return thanksD_cs.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @SuppressLint("WrongConstant")
        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            TextView class_txt = null;
            thanksD_A.ViewHolder viewHolder;
            if (view == null) {
                view = inflater.inflate(id, null);
                class_txt = (TextView) view.findViewById(R.id.tv_str);
                view.setTag(new ViewHolder(class_txt));
            } else {
                thanksD_A.ViewHolder viewHolder1 = (thanksD_A.ViewHolder) view.getTag(); // 重新获取ViewHolder
                class_txt = viewHolder1.class_txt;
            }
            thanksD_C vcs = (thanksD_C) thanksD_cs.get(i); // 获取当前项的实例
            class_txt.setText(vcs.getClass_txt().toString());
            return view;
        }

        private final class ViewHolder {
            TextView class_txt = null;

            public ViewHolder(TextView class_txt) {
                this.class_txt = class_txt;
            }

        }
    }


}
